package com.chenqq.qsocket.configuration;

import android.os.Environment;
import android.text.TextUtils;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.BehaviorSubject;
import io.reactivex.subscribers.DisposableSubscriber;

public class MinaConfigInfo {
    private MinaConfigInfo() {
        disposableHashMap = new HashMap<>();
        compositeDisposable = new CompositeDisposable();
    }

    private static MinaConfigInfo inStance;
    private HashMap<String, Disposable> disposableHashMap;
    private CompositeDisposable compositeDisposable;

    public static MinaConfigInfo getInStance() {
        if (inStance == null) {
            inStance = new MinaConfigInfo();
        }
        return inStance;
    }

    private Charset charset = StandardCharsets.UTF_8;
    private int sizePartSize = 30 * 1024;
    private boolean isNeedZip = true;
    private boolean isLog = true;
    private String pathDown = Environment.DIRECTORY_DOWNLOADS;
    private String temporaryPath = Environment.DIRECTORY_DOWNLOADS;

    public   String HeardAccept = "HeartAccept";//心跳包接受
    public   String HeardSend = "HeartSend";//心跳包发送


    public Charset getCharset() {
        return charset;
    }

    public MinaConfigInfo setCharset(Charset charset) {
        this.charset = charset;
        return this;
    }

    public int getSizePartSize() {

        return sizePartSize;
    }

    public MinaConfigInfo setSizePartSize(int sizePartSize) {
        this.sizePartSize = sizePartSize;
        return this;
    }

    public boolean isNeedZip() {
        return isNeedZip;
    }

    public MinaConfigInfo setNeedZip(boolean needZip) {
        isNeedZip = needZip;
        return this;
    }

    public String getPathDown() {
        return pathDown;
    }

    public MinaConfigInfo setPathDown(String pathDown) {
        this.pathDown = pathDown;
        return this;
    }

    public String getTemporaryPath() {
        return temporaryPath;
    }

    public MinaConfigInfo setTemporaryPath(String temporaryPath) {
        this.temporaryPath = temporaryPath;
        return this;
    }

    public boolean isLog() {
        return isLog;
    }

    public MinaConfigInfo setLog(boolean log) {
        isLog = log;
        return this;
    }

    public String getHeardAccept() {
        return HeardAccept;
    }

    public MinaConfigInfo setHeardAccept(String heardAccept) {
        HeardAccept = heardAccept;
        return this;
    }

    public String getHeardSend() {
        return HeardSend;
    }

    public MinaConfigInfo setHeardSend(String heardSend) {
        HeardSend = heardSend;
        return this;
    }

    public void addDisposable(String tag, Disposable disposable) {
        compositeDisposable.add(disposable);
        disposableHashMap.put(tag, disposable);
    }

    public void removeDisposable(Disposable disposable) {
        compositeDisposable.remove(disposable);
        if (disposableHashMap.containsValue(disposable)) {
            Iterator<Map.Entry<String, Disposable>> iterator =
                    disposableHashMap.entrySet().iterator();
            String tag = "";
            while (iterator.hasNext()) {
                Map.Entry<String, Disposable> next = iterator.next();
                if (next.getValue() == disposable) {
                    tag = next.getKey();
                    break;
                }
            }
            if (!TextUtils.isEmpty(tag)) {
                disposableHashMap.remove(tag);
            }
        }
        disposable.dispose();
    }

    public void removeDisposable(String tag) {
        Disposable disposable = disposableHashMap.get(tag);
        if (disposable != null) {
            disposableHashMap.remove(tag);
            disposable.dispose();
        }
    }
}
